import userApi from '@/config/server/api/user'
import dictMgr from '@/biz/building/dict.js'
import server from '@dt/server/dt'
import dt from '@dt/dt'

let key_userInfo = 'user_info'
let userInfo = dt.session.get(key_userInfo) || {}
let key_historyInfo = 'user_history'
let historyInfo = dt.storage.get(key_historyInfo) || {} //

function login(info) {
  return userApi.login
    .fetch({
      loginType: 3,
      loginInfoReq: info
    })
    .then((res) => {
      console.log(dictMgr, '^^^&&&')
      dictMgr.update() //刷新字典
      server.auth.setInfo({
        authorization: `${res.token_type} ${res.access_token}`,
        refreshToken: res.refresh_token,
        userId: res.userId
      })
      return userApi.info
        .fetch({
          userId: res.userId,
          deviceType: 'web'
        })
        .then((res) => {
          res = res.userInfo
          res.role = res.roleList.find((item) => item.roleId == info.roleId)
          userInfo = res
          dt.session.set(key_userInfo, res)

          // 将当前的手机号/工号存入缓存（缓存中没有的话）
          historyInfo.accounts = historyInfo.accounts || []
          if (historyInfo.accounts.indexOf(info.searchKey) < 0) {
            historyInfo.accounts.push(info.searchKey)
          }
          dt.storage.set(key_historyInfo, historyInfo)
          dt.session.set('isFirst', 1)
          return res
        })
    })
}

function logout() {
  return userApi.logout.fetch().then((res) => {
    userInfo = null
    dt.session.remove(key_userInfo)
    dt.session.remove('organListCopy')
    dt.session.remove('isFirst')
    server.auth.clear()
    dt.memory.clear()
  })
}

function roles(searchKey) {
  return userApi.roles
    .fetch({
      searchKey
    })
    .then((res) => {
      let list = res.list.filter((i) => i.loginWeb == 1)
      return list
    })
}

function code(phone, type) {
  return userApi.code
    .fetch({
      phone,
      type
    })
    .then((res) => {
      return res
    })
}

function imgCode() {
  return userApi.imgCode.fetch().then((res) => {
    return res
  })
}

function updatePwd({ userId, password, confirmPwd }) {
  return userApi.updatePwd
    .fetch({
      userId,
      password,
      confirmPwd
    })
    .then((res) => {
      dt.storage.set(key_historyInfo, historyInfo)
      userInfo.resetPassword = 'false'
      dt.session.set(key_userInfo, userInfo)
      return res
    })
}

function updatePwdCode({ phone, captcha }) {
  return userApi.updatePwdCode
    .fetch({
      phone,
      captcha
    })
    .then((res) => {
      return res
    })
}

function info() {
  return userInfo
}
dt.userInfo = info

function history() {
  return historyInfo
}

function getPersonMessUpdate(info) {
  return userApi.getPersonMessUpdate.fetch(info)
}

function getUserPwdExpireDate(info) {
  return userApi.getUserPwdExpireDate.fetch(info)
}

function checkPasswordFun(info) {
  return userApi.checkPassword.fetch(info)
}

function updatePwdCodeNoToken({ phone, captcha }) {
  return userApi.updatePwdCodeNoToken
    .fetch({
      phone,
      captcha
    })
    .then((res) => {
      return res
    })
}

function updatePwdNoToken({ phone, password, confirmPwd }) {
  return userApi.updatePwdNoToken
    .fetch({
      phone,
      password,
      confirmPwd
    })
    .then((res) => {
      return res
    })
}

export default {
  login,
  logout,
  roles,
  code,
  imgCode,
  updatePwd,
  updatePwdCode,
  info,
  history,
  getPersonMessUpdate,
  getUserPwdExpireDate,
  checkPasswordFun,
  updatePwdCodeNoToken,
  updatePwdNoToken
}
